package com.dbh.company.buildbook;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class LineIntersectionExample {
    public static void main(String[] args) {
        // 线段1的经纬度坐标
        double lon1 = 117.1915;
        double lat1 = 39.128958;
        double lon2 = 117.190974;
        double lat2 = 39.128604;

        // 线段2的经纬度坐标
        double lon3 = 117.191033;
        double lat3 = 39.128795;
        double lon4 = 117.191087;
        double lat4 = 39.129178;

        boolean hasIntersection = checkLineIntersection(lon1, lat1, lon2, lat2, lon3, lat3, lon4, lat4);

        System.out.println("Lines have intersection: " + hasIntersection);
    }

    public static boolean checkLineIntersection(double lon1, double lat1, double lon2, double lat2,
                                                double lon3, double lat3, double lon4, double lat4) {
        // 将经纬度转换为平面坐标系上的点
        Point2D.Double p1 = new Point2D.Double(lon1, lat1);
        Point2D.Double p2 = new Point2D.Double(lon2, lat2);
        Point2D.Double p3 = new Point2D.Double(lon3, lat3);
        Point2D.Double p4 = new Point2D.Double(lon4, lat4);

        // 判断线段是否相交
        Line2D.Double line1 = new Line2D.Double(p1, p2);
        Line2D.Double line2 = new Line2D.Double(p3, p4);

        return line1.intersectsLine(line2);
    }
}